package com.ylzinfo.jck.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

public abstract class BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    private String viewPrefix;

    public BaseController() {
        this.setViewPrefix(this.defaultViewPrefix());
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
    }

    public String viewName(String suffixName) {
        if (!suffixName.startsWith("/")) {
            suffixName = "/" + suffixName;
        }

        return this.getViewPrefix() + suffixName;
    }

    public String getViewPrefix() {
        return this.viewPrefix;
    }

    public void setViewPrefix(String viewPrefix) {
        if (viewPrefix.startsWith("/")) {
            viewPrefix = viewPrefix.substring(1);
        }

        this.viewPrefix = viewPrefix;
    }

    protected String defaultViewPrefix() {
        String currentViewPrefix = "";
        RequestMapping requestMapping = (RequestMapping)AnnotationUtils.findAnnotation(this.getClass(), RequestMapping.class);
        if (requestMapping != null && requestMapping.value().length > 0) {
            currentViewPrefix = requestMapping.value()[0];
        }

        return currentViewPrefix;
    }

    protected void addMessage(Model model, String... messages) {
        StringBuilder sb = new StringBuilder();
        String[] var4 = messages;
        int var5 = messages.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String message = var4[var6];
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }

        model.addAttribute("message", sb.toString());
    }

    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
        StringBuilder sb = new StringBuilder();
        String[] var4 = messages;
        int var5 = messages.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String message = var4[var6];
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }

        redirectAttributes.addFlashAttribute("message", sb.toString());
    }

    protected String redirectTo(String url) {
        StringBuffer rto = new StringBuffer("redirect:");
        rto.append(url);
        return rto.toString();
    }
}
